package com.financing.platform.task;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.financing.mail.dto.MailContentDTO;
import com.financing.mail.service.MailSenderService;
import com.financing.platform.dto.XcxSearchnoRecordDTO;
import com.financing.platform.service.IXcxSearchnoRecordService;

/**
 * 
 * @Description: 微信小程序邮件下发
 */
@Component(value = "mailWechatXcx")
public class MailWechatXcx {

	Logger log = Logger.getLogger(this.getClass());


	@Resource
	private MailSenderService mailSenderService;

	@Autowired
	private IXcxSearchnoRecordService xcxSearchnoRecordService;

	private static boolean runTimeFlag = false;
	 //9,12,17
	@Scheduled(cron = "0 0 5,12,17 * * ?")
	public void sendMessageJob() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		System.out.println("开始发送邮件："+sdf.format(new Date()));
		if (runTimeFlag) {
			return;
		} else {
			runTimeFlag = true;
		}
		try {
//			String content="";
			StringBuffer mailContentDetail = new StringBuffer();
			XcxSearchnoRecordDTO xcxSearchnoRecord = new XcxSearchnoRecordDTO();
			xcxSearchnoRecord.setIsGetMail(1);
			xcxSearchnoRecord.setFeedbackStatus("0");
			List<XcxSearchnoRecordDTO> messageList = xcxSearchnoRecordService.queryList(xcxSearchnoRecord);
			mailContentDetail.append("<table border=\"1\" cellspacing=\"0\" cellpadding=\"10\"><tr><td>微信昵称</td><td>搜索关键字</td><td>产品添加状态</td></tr>");
			for (XcxSearchnoRecordDTO message : messageList) {
				xcxSearchnoRecord.setId(message.getId());
				xcxSearchnoRecord.setIsGetMail(2);
				xcxSearchnoRecordService.update(xcxSearchnoRecord);
				mailContentDetail.append("<tr><td>")
				.append(message.getWxUsername())
				 .append("</td><td>")
				 .append(message.getKeyword())
				 .append("</td><td></td></tr>");
			}
			mailContentDetail.append("</table>");
			//发送邮件 wechatXcxMail
			if(messageList.size()>0){
				messageSend(mailContentDetail.toString());
			}
		} catch (SQLException e) {
			log.warn("update xcxSearchnoRecord Message status error:", e);
		}
		
		runTimeFlag = false;
		log.info("end send mail message job....");
	}

	/**
	 * 
	 * @Title: messageSend @Description: 发送邮件 @param message @return
	 *         void @throws
	 */
	private void messageSend(String message) {
		String CC = "guozongxun@superfa.cn,duyouwei@superfa.cn,fangxu@superfa.cn,sanghaiqin@superfa.cn";
		// 创建邮件
		MailContentDTO mail = new MailContentDTO();
		mail.setSubject("小程序搜索未果列表");
		mail.setContext(message);
//		mail.setCc(CC);   // 抄送     只能抄送一个
		for (int i = 0; i < CC.split(",").length; i++) {
			mail.setRecAddress(CC.split(",")[i]); // 接受者
			try {
				mailSenderService.send(mail);	
			} catch (Exception e) {
				// TODO: handle exception
				log.error("send email error:",e);
			}
				
		}
	}
}
